package com.huangyi;

import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        //基本计算器II
        class Solution {
            public int calculate(String s) {
                Stack<Integer> stack = new Stack<>();
                int n = s.length();
                char[] ch = s.toCharArray();
                char op = '+'; // 上一个运算符，默认'+'

                for (int i = 0; i < n; i++) {
                    if (ch[i] == ' ') continue; // 跳过空格

                    if (Character.isDigit(ch[i])) {
                        // 读多位数字：区间 [i, j)
                        int j = i, num = 0;
                        while (j < n && Character.isDigit(ch[j])) {
                            num = num * 10 + (ch[j] - '0');
                            j++;
                        }

                        // 按上一个运算符把 num 落到栈里
                        if (op == '+') {
                            stack.push(num);
                        } else if (op == '-') {
                            stack.push(-num);
                        } else if (op == '*') {
                            stack.push(stack.pop() * num);
                        } else if (op == '/') {
                            stack.push(stack.pop() / num); // 向0取整
                        }

                        i = j - 1; // 跳到数字末尾，外层for会再+1
                    } else if (ch[i] == '+' || ch[i] == '-' || ch[i] == '*' || ch[i] == '/') {
                        op = ch[i]; // 只有四则符号时才更新
                    }
                    // 其他字符（理论上不会出现）忽略
                }

                int res = 0;
                while (!stack.isEmpty()) res += stack.pop();
                return res;
            }
        }

        //字符串解码
        class Solution2 {
            public String decodeString(String ss) {
                char[] s = ss.toCharArray();
                int n = s.length;

                Stack<StringBuilder> stringStack = new Stack<>();
                stringStack.push(new StringBuilder()); // 根层：收集最外层结果
                Stack<Integer> intStack = new Stack<>();

                int i = 0;
                while (i < n) {
                    if (s[i] >= '0' && s[i] <= '9') {
                        // 读多位数字，压入次数栈
                        int tmp = 0;
                        while (i < n && s[i] >= '0' && s[i] <= '9') {
                            tmp = tmp * 10 + (s[i] - '0');
                            i++;
                        }
                        intStack.push(tmp);
                    } else if (s[i] == '[') {
                        // '[' 后面紧跟的连续字母，作为“新层的起始段”压入字符串栈
                        i++;
                        StringBuilder tmp = new StringBuilder();
                        while (i < n && s[i] >= 'a' && s[i] <= 'z') {
                            tmp.append(s[i]);
                            i++;
                        }
                        stringStack.push(tmp);
                    } else if (s[i] == ']') {
                        // 弹出次数与段，把段重复并追加到上一层
                        int tmpInt = intStack.pop();
                        StringBuilder tmp = stringStack.pop();      // 当前层段
                        StringBuilder top = stringStack.peek();     // 上一层段
                        while (tmpInt > 0) {
                            top.append(tmp);
                            tmpInt--;
                        }
                        i++;
                    } else {
                        // 普通字母：收集连续字母并追加到当前层
                        StringBuilder tmp = new StringBuilder();
                        while (i < n && s[i] >= 'a' && s[i] <= 'z') {
                            tmp.append(s[i]);
                            i++;
                        }
                        stringStack.peek().append(tmp);
                    }
                }
                return stringStack.pop().toString();
            }
        }
    }
}